<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/factory/CommandFactory.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/utilities/fpdf/fpdf.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];
	
	class PresenterBill {
		/**
		 * Constructor of the presenter
		 */
		public function __construct() {}
		
		/**
		 * Method to execute when user clicks on creating a new bill
		 */
		public function clickCreateBill($billForm)  {
			$address = explode(", ", $billForm['address']);
			$country = $address[3];
			$stateProvince = $address[2];
			$city = $address[1];
			$zone = $address[0];
			
			$phones = explode("\n", $billForm['phones']);
			$firstPhone = explode(": ", $phones[0]);
			$homePhone = $firstPhone[1];		
			$secondPhone = explode(": ", $phones[1]);
			$mobilePhone = $secondPhone[1];		
			$thirdPhone = explode(": ", $phones[2]);
			$workPhone = $thirdPhone[1];
			$fourthPhone = explode(": ", $phones[3]);
			$faxPhone = $fourthPhone[1];
			
			$productsTextarea = explode("\n", $billForm['products']);
			for ($i = 0; $i < count($productsTextarea); $i++) {
				$productFeatures = explode(", ", $productsTextarea[$i]);
				$products['pk_line'][$i] = $i + 1;
				$productUnitsQuantity = explode(" ", $productFeatures[2]);
				$products['quantity'][$i] = $productUnitsQuantity[0];
				$products['units'][$i] = $productUnitsQuantity[1];
				$productUnitCost = explode(' ', substr($productFeatures[3], 4));
				$products['unit_cost'][$i] = $productUnitCost[0];
				$products['bill_number'][$i] = $billForm['number'];
				$products['bill_control_number'][$i] = $billForm['controlNumber'];
				$products['pk_code'][$i] = substr($productFeatures[0], 5);
				$products['branch_rif'][$i] = 'J-31099802-1';
			}
			
			$command = CommandFactory :: createCommandCreateBill($billForm['ciRifList'], 
				$billForm['ciRif'], $billForm['clientName'], $billForm['secondName'], 
				$billForm['firstLastName'], $billForm['secondLastName'], 
				$billForm['emailAddress'], date("Y-m-d", time()), '', $country, $stateProvince,
				$city, $zone, $homePhone, $mobilePhone, $workPhone, $faxPhone, 
				$billForm['number'], $billForm['controlNumber'], $billForm['billDate'], 
				$billForm['tax'], $products);
			$command -> execute();		
			return $command -> getResult();
		}
		
		/**
		 * Method to execute to load all existing products
		 */
		public function loadExistingProducts() {
			$command = CommandFactory :: createCommandSelectProducts();
			$command -> execute();
			$products = $command -> getResult();
			
			$result = '<select name="product" onchange="xajax_updateProductFeatures(document.billProducts.product.options[document.billProducts.product.selectedIndex].value)">';
			$result .= '<option value="0">Seleccione un producto</option>';
			$i = 0;
			
			while ($i < count($products['pk_code'])) {
				$result .= '<option value="' . $products['pk_code'][$i] . '">' . 
					$products['name'][$i];
				
				if (strcmp($products['specifications'][$i], '') != 0) {
					$result .= '&nbsp;(' . $products['specifications'][$i] . ')';
				}
				
				$result .= '</option>';
				
				$i++;
			}
			
			$result .= '</select>';
			
			return $result;
		}
		
		/**
		 * Method to execute to update product features
		 */
		public function updateProductFeatures($pkCode) {
			$command = CommandFactory :: createCommandSelectProductByCode($pkCode);
			$command -> execute();
			$product = $command -> getResult();
			
			$result = '<input type="hidden" id="productName" value="' . $product['name'][0] . '" />';
			$result .= '<input type="text" id="quantity" size="8" maxlength="8" value="0" onblur="updateTotalCost(document.billProducts.quantity.value, document.billProducts.unitCost.value)" />&nbsp;';
			$result .= '<input name="productUnits" size="8" maxlength="8" readonly="readonly" 
				value="' . $product['product_units'][0] . '" />&nbsp;';
			$result .= '<input type="button" name="buttonAddProduct" value="Agregar" 
				onclick="addProduct(
document.billProducts.product.options[document.billProducts.product.selectedIndex].value,
document.billProducts.productName.value,
document.billProducts.quantity.value, document.billProducts.productUnits.value, 
document.billProducts.unitCost.value, document.billProducts.unitCostCurrency.value)" />#separator#';
			$result .= '<input type="text" id="unitCost" size="8" maxlength="15" 
				readonly="readonly" value="' . $product['unit_cost'][0] . '" />&nbsp;';
			$result .= '<input type="text" id="unitCostCurrency" size="8" maxlength="20" 
				readonly="readonly" value="' . $product['unit_cost_currency'][0] . 
				'" />#separator#';
			$result .= '<input type="text" id="totalCost" size="8" maxlength="15" 
				readonly="readonly" value="0" />&nbsp;';
			$result .= '<input type="text" id="totalCostCurrency" size="8" maxlength="20" 
				readonly="readonly" value="' . $product['unit_cost_currency'][0] . 
				'" />';
				
			return $result;
		}
		
		/**
		 * Method to execute to select an existing client
		 */
		public function selectExistingClient($clientType, $ciRif) {
			if (strcmp($clientType, 'ci') == 0)
				$command = CommandFactory :: createCommandSelectExistingNaturalCustomer($ciRif);
			
			else
				$command = CommandFactory :: createCommandSelectExistingLegalClient($ciRif);
			
			$command -> execute();
			$result = $command -> getResult();
			
			$client = '<input type="text" name="clientName" size="32" maxlength="30" 
				value="' . $result['name'] . '" />#separator#';
			$client .= '<input type="text" name="secondName" size="32" maxlength="20" 
				value="' . $result['second_name'] . '" />#separator#';
			$client .= '<input type="text" name="firstLastName" size="32" maxlength="20" 
				value="' . $result['first_last_name'] . '" />#separator#';
			$client .= '<input type="text" name="secondLastName" size="32" maxlength="20" 
				value="' . $result['second_last_name'] . '" />#separator#';
			$client .= '<input type="text" name="emailAddress" size="32" maxlength="40" 
				value="' . $result['email_address'] . '" />#separator#';
			$client .= '<textarea name="address" cols="21" rows="5" readonly="readonly" 
				class="modalInput" rel="#promptAddress">' . $result['address'] . 
				'</textarea>#separator#';
			$client .= '<textarea name="phones" cols="21" rows="5" readonly="readonly"
				class="modalInput" rel="#promptPhones">' . $result['phones'] . '</textarea>';			
			
			return $client;
		}
		
				/**
		 * Method to execute to select Bills  client*/
		public function selectBills() {
					$GLOBALS['debug']->log('Inicio funcion select bills--presentador');
			$command = CommandFactory :: createCommandSelectBills();
			
			$command -> execute();
			$bills = $command -> getResult();
			
			$result = '<table class="formTable" cellspacing="0" cellpadding="5">';
			$result .= '<tr class="odd">';
			$result .= '<td align="center"><b>Numero de Control</b></td>';
			$result .= '<td align="center"><b>Fecha</b></td>';
			$result .= '<td align="center"><b>Rif</b></td>';
			$result .= '<td align="center"><b>Nombre</b></td>';
			$result .= '<td align="center"><b>Correo</b></td>';

			$result .= '</tr>';
			$i = 0;
			while ($i < count($bills['pk_number'])) {
				if ($i % 2 == 0) $result .= '<tr class="even">';
				else $result .= '<tr class="odd">';
				
				$result .= '<td align="center">' . $bills['pk_control_number'][$i] . '</td>';
				$result .= '<td align="center">' . $bills['date'][$i] . '</td>';
				$result .= '<td align="center">' . $bills['pk_rif'][$i] . '</td>';
				$result .= '<td align="center">' . $bills['name'][$i] . '</td>';
				$result .= '<td align="center">' . $bills['email_address'][$i] . '</td>';
				$result .= '<td align="center"><a href="#" onclick="xajax_selectBillDetails(' . $bills['pk_number'][$i] . ', ' . $bills['pk_control_number'][$i++] . ')">Ver</a></td>';
	
				$result .= '</tr>';
			}
			

			$result .= '</table>';
		
			return $result;
		}
		
				/**
		 * Method to execute to select given bills details client
		 */
		public function selectBillDetails($number, $controlNumber) {
			$command = CommandFactory :: createCommandSelectBillDetails
				($number, $controlNumber);
			$command -> execute();
			$products = $command -> getResult();
			
					
		    $GLOBALS['debug']->log($controlNumber,'Numero de control');
			
			$result = '<table class="formTable" cellspacing="0" cellpadding="5">';
			$result .= '<tr class="odd">';
			$result .= '<td align="center"><b>Rengl&oacute;n</b></td>';
			$result .= '<td align="center"><b>Descripci&oacute;n</b></td>';
			$result .= '<td align="center"><b>Unidades</b></td>';
			$result .= '<td align="center"><b>Cantidad</b></td>';
			$result .= '<td align="center"><b>Costo unitario</b></td>';
			$result .= '<td align="center"><b>Total</b></td>';
			$result .= '</tr>';
			$i = 0; $subtotal = 0;
			while ($i < count($products['pk_line'])) {
				if ($i % 2 == 0) $result .= '<tr class="even">';
				else $result .= '<tr class="odd">';
					
				$subtotal += $products['total'][$i];
				
				$result .= '<td align="center">' . $products['pk_line'][$i] . '</td>';
				$result .= '<td align="center">' . $products['name'][$i] . '</td>';
				$result .= '<td align="center">' . $products['units'][$i] . '</td>';
				$result .= '<td align="center">' . $products['quantity'][$i] . '</td>';
				$result .= '<td align="center">' . $products['unit_cost'][$i] . '</td>';
				$result .= '<td align="center">' .  $products['total'][$i++] . '</td>';
				
				$result .= '</tr>';
			}
			
			if ($i % 2 == 0) $result .= '<tr class="even">';
			else $result .= '<tr class="odd">';			
			$result .= '<td colspan="5" align="right">Subtotal</td>';
			$result .= '<td align="center">' . $subtotal . '</td>';
			$result .= '</tr>'; $i++;
			
			if ($i % 2 == 0) $result .= '<tr class="even">';
			else $result .= '<tr class="odd">';	
			$result .= '<td colspan="5" align="right">' . $products['tax'][0] . '% IVA</td>';
			$result .= '<td align="center">' . $subtotal * ($products['tax'][0] / 100) . '</td>';
			$result .= '</tr>'; $i++;
			
			if ($i % 2 == 0) $result .= '<tr class="even">';
			else $result .= '<tr class="odd">';			
			$result .= '<td colspan="5" align="right">Total</td>';
			$result .= '<td align="center">' . $subtotal * (1 + $products['tax'][0] / 100) . '</td>';
			$result .= '</tr>';
			
			$result .= '</table><br />';
			
			$result .= '<div align="center">';
			$result .= '<a href="#" 
				onclick="xajax_selectBills()">Regresar</a>';
			$result .= '</div>';
			
			
			return $result;
		}
		
	/**
		 * Method to execute to select Bills ductobarra*/
		public function selectBillsDuctobarra() {
					$GLOBALS['debug']->log('Inicio funcion select bills ductobarra--presentador');
			$command = CommandFactory :: createCommandSelectBillsDuctobarra();
			
			$command -> execute();
			$bills = $command -> getResult();
			
			$result = '<table class="formTable" cellspacing="0" cellpadding="5">';
			$result .= '<tr class="odd">';
			$result .= '<td align="center"><b>Numero de Control</b></td>';
			$result .= '<td align="center"><b>Fecha</b></td>';
			$result .= '<td align="center"><b>CI</b></td>';
			$result .= '<td align="center"><b>Nombre</b></td>';
			$result .= '<td align="center"><b>Apellido</b></td>';
			$result .= '<td align="center"><b>Correo</b></td>';

			$result .= '</tr>';
			$i = 0;
			while ($i < count($bills['pk_number'])) {
				if ($i % 2 == 0) $result .= '<tr class="even">';
				else $result .= '<tr class="odd">';
				
				$result .= '<td align="center">' . $bills['pk_control_number'][$i] . '</td>';
				$result .= '<td align="center">' . $bills['date'][$i] . '</td>';
				$result .= '<td align="center">' . $bills['pk_id_number'][$i] . '</td>';
				$result .= '<td align="center">' . $bills['first_name'][$i] . '</td>';
				$result .= '<td align="center">' . $bills['first_last_name'][$i] . '</td>';
				$result .= '<td align="center">' . $bills['email_address'][$i] . '</td>';
				$result .= '<td align="center"><a href="#" onclick="xajax_selectBillsDetailsDuctobarra(' . $bills['pk_number'][$i] . ', ' . $bills['pk_control_number'][$i++] . ')">Ver</a></td>';
	
				$result .= '</tr>';
			}
			

			$result .= '</table>';
		
			return $result;
		}
		
				/**
		 * Method to execute to select given bills details ductobarra
		 */
		public function selectBillsDetailsDuctobarra($number, $controlNumber) {
			$command = CommandFactory :: createCommandSelectBillDetails
				($number, $controlNumber);
			$command -> execute();
			$products = $command -> getResult();
			
					
		    $GLOBALS['debug']->log($controlNumber,'Numero de control');
			
			$result = '<table class="formTable" cellspacing="0" cellpadding="5">';
			$result .= '<tr class="odd">';
			$result .= '<td align="center"><b>Rengl&oacute;n</b></td>';
			$result .= '<td align="center"><b>Descripci&oacute;n</b></td>';
			$result .= '<td align="center"><b>Unidades</b></td>';
			$result .= '<td align="center"><b>Cantidad</b></td>';
			$result .= '<td align="center"><b>Costo unitario</b></td>';
			$result .= '<td align="center"><b>Total</b></td>';
			$result .= '</tr>';
			$i = 0; $subtotal = 0;
			while ($i < count($products['pk_line'])) {
				if ($i % 2 == 0) $result .= '<tr class="even">';
				else $result .= '<tr class="odd">';
					
				$subtotal += $products['total'][$i];
				
				$result .= '<td align="center">' . $products['pk_line'][$i] . '</td>';
				$result .= '<td align="center">' . $products['name'][$i] . '</td>';
				$result .= '<td align="center">' . $products['units'][$i] . '</td>';
				$result .= '<td align="center">' . $products['quantity'][$i] . '</td>';
				$result .= '<td align="center">' . $products['unit_cost'][$i] . '</td>';
				$result .= '<td align="center">' .  $products['total'][$i++] . '</td>';
				
				$result .= '</tr>';
			}
			
			if ($i % 2 == 0) $result .= '<tr class="even">';
			else $result .= '<tr class="odd">';			
			$result .= '<td colspan="5" align="right">Subtotal</td>';
			$result .= '<td align="center">' . $subtotal . '</td>';
			$result .= '</tr>'; $i++;
			
			if ($i % 2 == 0) $result .= '<tr class="even">';
			else $result .= '<tr class="odd">';	
			$result .= '<td colspan="5" align="right">' . $products['tax'][0] . '% IVA</td>';
			$result .= '<td align="center">' . $subtotal * ($products['tax'][0] / 100) . '</td>';
			$result .= '</tr>'; $i++;
			
			if ($i % 2 == 0) $result .= '<tr class="even">';
			else $result .= '<tr class="odd">';			
			$result .= '<td colspan="5" align="right">Total</td>';
			$result .= '<td align="center">' . $subtotal * (1 + $products['tax'][0] / 100) . '</td>';
			$result .= '</tr>';
			
			$result .= '</table><br />';
			
			$result .= '<div align="center">';
			$result .= '<a href="#" 
				onclick="xajax_selectBillsDuctobarra()">Regresar</a>';
			$result .= '</div>';
			
			
			return $result;
		}
		
	
			
	}
?>